package array;
//数组排序
public class Sorting {
    public static void main(String[] args) {
        // 设 names 数组中存放学生姓名
        String[] names = { "雷军" , "马云" , "谭浩强" , "佟瑶" , "蓝馨怡" };
        // 设 courses 数组中依次存放三门课程的名称
        String[] courses = { "C++" , "Java" , "English","Total Score"};
        // 设 scores 数组中依次存储的是 names 数组中各个学生的 C++ 、Java 、English 课程的成绩
        int[][] scores = {
                {  90 ,  89 ,  75 } ,
                {  59 ,  40 , 100 } ,
                { 100 ,  99 ,  80 } ,
                {  80 ,  61 ,  61 } ,
                {  60 , 100 ,  99 } ,
        };
        //计算总成绩
        int[][] totalScore = new int[scores.length][scores[0].length+1];
        int sum = 0;
        for (int i = 0; i < totalScore.length; i++) {
            for (int j = 0; j < totalScore[0].length; j++) {
                if (j==totalScore[0].length-1) {
                    totalScore[i][j] = sum ;
                    sum =0;
                    break;
                }
                totalScore[i][j] = scores[i][j];
                sum += scores[i][j];
            }
        }
        //遍历输出totalScore
        for( int i = 0 ; i < totalScore.length - 1 ; i++ ){
            for( int j = 0 ; j < totalScore.length - 1 - i ; j++ ){
                int f = totalScore[ j ][ 3] ;
                int s = totalScore[ j + 1 ][ 3 ] ;
                if( f < s ) {
                    int[] temp = totalScore[ j ] ;
                    totalScore[ j ] = totalScore[ j + 1 ];
                    totalScore[ j + 1 ] = temp ;
                    String x = names[ j ];
                    names[ j ] = names[ j + 1 ];
                    names[ j + 1 ] = x ;
                }
            }
        }
        for( int i = 0 ; i < totalScore.length ; i++ ){
            System.out.print(  names[ i ] + " => " ); // 输出学生姓名
            for( int j = 0 ; j < totalScore[ i ] .length ; j++ ){
                System.out.print(  courses[ j ] + ":");// 输出课程名称
                System.out.print(  totalScore[ i ][ j ] ); // 输出课程成绩
                if( j < totalScore[ i ] .length - 1 ) {
                    System.out.print( " , " );
                }
            }
            System.out.println();
        }


    }
}
